package com.wanhe.shoppingcart.counter;

import android.text.TextUtils;

import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.holder.DishHolder;
import com.wanhe.base.util.Logger;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.shoppingcart.manager.ICartItem;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by XieZaiWei on 2017/5/9.
 * 菜单加菜管理类
 */

public class CounterManager {

	public String err;
	private Map<String, ICounter> counterMap = new HashMap<>();

	private static CounterManager ourInstance;

	private CounterManager() {
		initialization();
	}

	private static CounterManager getInstance() {
		if (ourInstance == null) {
			ourInstance = new CounterManager();
		}
		return ourInstance;
	}

	/**
	 * 生成计数器和控件分类的关联关系
	 *
	 * @author 陈德基
	 * created at 2016/11/4 14:41
	 */
	private void initTypeMapping() {
		CounterConfig.getInstance().init();
	}

	/**
	 * 初始化菜品添加器工厂
	 *
	 * @author 陈德基
	 * created at 2016/11/4 14:36
	 */
	public void initialization() {
		initTypeMapping();
		this.addCounterMap(new OnlyOneExistCounter(new BaseCounter()))
				.addCounterMap(new ShowSecLevelDialogCounter(new BaseCounter()))
				.addCounterMap(new AddFourTimeCounter(new BaseCounter()));
	}

	/**
	 * 检测购物车中是否有没完成的菜品添加操作
	 *
	 * @author 陈德基
	 * create at 2017/4/27 16:50
	 */
	public boolean isCartFilled() {
		boolean isFilled = true;
		for (Map.Entry<String, ICounter> entry : counterMap.entrySet()) {
			isFilled = entry.getValue().isCountFilled();
			if (!isFilled) {
				break;
			}
		}
		return isFilled;
	}

	public ICounter getCounter(String type) {
		String counterType = CounterConfig.getInstance().getCounterType(type);
		return counterMap.get(counterType);
	}

	private CounterManager addCounterMap(ICounter counter) {
		counterMap.put(counter.getCounterType(), counter);
		return this;
	}

	/**
	 * 添加购物项目
	 *
	 * @param dishId
	 */
	public void add(String dishId) {
		Dish dish = DishHolder.getInstance().get(dishId);
		if (!ObjectUtil.checkObject(dish)) {
			return;
		}
		//获取特殊菜品的特殊添加数量的策略计数器
		getCounter(getCounterType(dish)).plus(dishId, true);
	}

	/**
	 * 添加购物项目
	 *
	 * @param dishId
	 * @param isNeedSyncToServer
	 */
	public void add(String dishId, boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(dishId);
		if (!ObjectUtil.checkObject(dish)) {
			return;
		}
		//获取特殊菜品的特殊添加数量的策略计数器
		getCounter(getCounterType(dish)).plus(dishId, isNeedSyncToServer);
	}

	public void add(ICartItem cartItem) {
		Dish dish = DishHolder.getInstance().get(cartItem.getDishGuid());
		getCounter(getCounterType(dish)).plus(cartItem, true);
	}

	public void add(ICartItem cartItem, boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(cartItem.getDishGuid());
		getCounter(getCounterType(dish)).plus(cartItem, isNeedSyncToServer);
	}

	/**
	 * 减少购物车中某个购物项目的数量
	 *
	 * @author 陈德基
	 * created at 2016/8/5 10:57
	 */
	public void reduce(String dishId) {
		Logger.i(Logger.TAG, "==11====reduce===" + dishId);
		Dish dish = DishHolder.getInstance().get(dishId);
		if (!ObjectUtil.checkObject(dish)) {
			return;
		}
		//获取特殊菜品的特殊添加数量的策略计数器
		getCounter(getCounterType(dish)).reduce(dishId, true);
	}

	/**
	 * 减少购物车中某个购物项目的数量
	 *
	 * @author 陈德基
	 * created at 2016/8/5 10:57
	 */
	public void reduce(String dishId, boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(dishId);
		if (!ObjectUtil.checkObject(dish)) {
			return;
		}
		//获取特殊菜品的特殊添加数量的策略计数器
		getCounter(getCounterType(dish)).reduce(dishId, isNeedSyncToServer);
	}

	public void reduce(ICartItem cartItem) {
		Dish dish = DishHolder.getInstance().get(cartItem.getDishGuid());
		getCounter(getCounterType(dish)).reduce(cartItem, true);
	}

	public void reduce(ICartItem cartItem, boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(cartItem.getDishGuid());
		getCounter(getCounterType(dish)).reduce(cartItem, isNeedSyncToServer);
	}

	/**
	 * 获取计数器的类型
	 *
	 * @author 陈德基
	 * create at 2017/1/5 20:33
	 */
	private String getCounterType(Dish dish) {
		//需要根据菜品的特殊属性txt1来进行判断，添加到购物车的规则
		String type = dish.getTxt1();
		Logger.i(Logger.TAG, "=====getCounterType====" + type);
		if (TextUtils.isEmpty(type)) {
			type = CounterConfig.BASE_DISH_TYPE;
		} else if (type.startsWith(CounterConfig.GIF_ARR_DISH_TYPE)) {
			type = CounterConfig.GIF_ARR_DISH_TYPE;
		} else if (type.contains(CounterConfig.ADD_ONCE_FOUR_TIMES_TYPE)) {
			type = CounterConfig.ADD_ONCE_FOUR_TIMES_TYPE;
		} else if (type.contains(CounterConfig.CHUIYU_TAOCAN_TYPE)) {
			type = CounterConfig.CHUIYU_TAOCAN_TYPE;
		} else {
			type = CounterConfig.BASE_DISH_TYPE;
		}
		return type;
	}
}
